package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.specificationOption.TbSpecificationOption;
import com.dongdongshop.pojo.specificationOption.TbSpecificationOptionExample;
import com.dongdongshop.service.specificationOption.SpecificationOptionService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Component
public class SpecificationOptionServiceImpl implements SpecificationOptionService {
    @Resource
    private TbSpecificationOptionMapper specificationOptionMapper;

    /**
     * 批量添加规格选项
     *
     * @param
     */
    @Override
    public void addSpecificationOptionBatch(List<TbSpecificationOption> list) {
        specificationOptionMapper.addSpecificationOptionBatch(list);
    }
    /**
     * 根据规格id查询该规格所拥有的所有规格选项信息
     */
    @Override
    public List<TbSpecificationOption> getspecificationOptionsBySpecificationId(Long id) {
        List<TbSpecificationOption> list = specificationOptionMapper.getspecificationOptionsBySpecificationId(id);
        return list;
    }
    /**
     * 根据规格id修改规格选项信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSpecificationOptionBySpecificationId(List<TbSpecificationOption> list, Long specId) {
        //先删除改该规格在规格选项表中的所有
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(specId);
        specificationOptionMapper.deleteByExample(example);

        //批量添加新的规格选项数据
        specificationOptionMapper.addSpecificationOptionBatch(list);
    }
    /**
     * 根据规格id删除规格选项的信息
     */
    @Override
    public void deleteSpecificationOptionBySpecificationId(Long id) {
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        specificationOptionMapper.deleteByExample(example);
    }
}
